package com.wendy.admin.controller;

import com.wendy.admin.domain.bo.SysMenuBo;
import com.wendy.admin.domain.vo.RouterVo;
import com.wendy.admin.domain.vo.SysMenuVo;
import com.wendy.admin.domain.vo.TreeSelect;
import com.wendy.admin.entity.SysMenu;
import com.wendy.admin.service.ISysMenuService;
import com.wendy.common.core.constant.UserConstants;
import com.wendy.common.core.errcode.menu.MenuError;
import com.wendy.common.core.utils.StringUtils;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.custom.security.utils.SecurityUtils;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息
 * 
 * @author wendy
 */
@RestController
@RequestMapping("/menu")
public class SysMenuController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    /**
     * 获取菜单列表
     */
    @RequiresPermissions("system:menu:list")
    @GetMapping("/list")
    public List<SysMenuVo>  list(SysMenuBo menu)
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return MapstructUtils.convert(menus,SysMenuVo.class);
    }

    /**
     * 根据菜单编号获取详细信息
     */
    @RequiresPermissions("system:menu:query")
    @GetMapping(value = "/{menuId}")
    public SysMenuVo getInfo(@PathVariable Long menuId)
    {
        return MapstructUtils.convert(menuService.selectMenuById(menuId), SysMenuVo.class) ;
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public List<TreeSelect>  treeselect(SysMenuBo menu)
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(menu, userId);
        return menuService.buildMenuTreeSelect(menus);
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public Map<String, Object> roleMenuTreeselect(@PathVariable("roleId") Long roleId)
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuList(userId);
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        ajax.put("menus", menuService.buildMenuTreeSelect(menus));
        return ajax;
    }

    /**
     * 新增菜单
     */
    @RequiresPermissions("system:menu:add")
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public int add(@Validated @RequestBody SysMenu menu)
    {
        if (!menuService.checkMenuNameUnique(menu))
        {
            MenuError.ADD_ERROR.formatFire(menu.getMenuName());
        }
        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
        {
            MenuError.ADD_HTTP_ERROR.formatFire(menu.getMenuName());
        }
        menu.setCreateBy(SecurityUtils.getUsername());
        return menuService.insertMenu(menu);
    }

    /**
     * 修改菜单
     */
    @RequiresPermissions("system:menu:edit")
    @Log(title = "菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Integer edit(@Validated @RequestBody SysMenu menu)
    {
        if (!menuService.checkMenuNameUnique(menu))
        {
            MenuError.EDIT_ERROR.formatFire(menu.getMenuName());
        }
        else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.ishttp(menu.getPath()))
        {
            MenuError.EDIT_HTTP_ERROR.formatFire(menu.getMenuName());
        }
        else if (menu.getMenuId().equals(menu.getParentId()))
        {
            MenuError.EDIT_SELF_ERROR.formatFire(menu.getMenuName());
        }
        menu.setUpdateBy(SecurityUtils.getUsername());
        return menuService.updateMenu(menu);
    }

    /**
     * 删除菜单
     */
    @RequiresPermissions("system:menu:remove")
    @Log(title = "菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public int remove(@PathVariable("menuId") Long menuId)
    {
        if (menuService.hasChildByMenuId(menuId))
        {
            MenuError.DEL_SUB_ERROR.fire();
        }
        if (menuService.checkMenuExistRole(menuId))
        {
            MenuError.DEL_IN_USED_ERROR.fire();
        }
        return menuService.deleteMenuById(menuId);
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public List<RouterVo>  getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return menuService.buildMenus(menus);
    }
}